{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CanteraUq\n",
    "\n",
    "Xingyu Su [suxy15tsinghua@gmail.com](suxy15tsinghua@gmail.com)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Mechanism reduction\n",
    "\n",
    "Firstly, a series of skeletal mechanisms should be generated. We used the directed relation graph (DRG) method implemented in [PyMARS](https://github.com/Niemeyer-Research-Group/pyMARS) here. DRG method is developed by T. Lu[<sup>1</sup>](#Lu2005) and using the relation between species to delete unessential species.\n",
    "$$\n",
    "\\begin{equation}\n",
    "    r_{mn}=\\frac{\\sum_{i=1}^{N_R}|\\nu_{i,m}\\omega_i\\delta_n^i|}{\\sum_{i=1}^{N_R}|\\nu_{i,m}\\omega_i|}\n",
    "\\end{equation}\n",
    "$$\n",
    "where\n",
    "$$\n",
    "\\begin{equation}\n",
    "    \\delta_i^n=\n",
    "\\begin{cases}\n",
    "\t1,& \\text{if the} \\ i_{th}\\ \\text{reaction involves species}\\ n\\\\\n",
    "\t0,& \\text{otherwise}\n",
    "\\end{cases}\n",
    "\\end{equation}\n",
    "$$\n",
    "\n",
    "A detailed mechanism of dimethyl ether (DME) is chosen from [LLNL](https://combustion.llnl.gov/mechanisms/dimethyl-ether) and involves 79 species in 660 reactions. And its ignition delay time under different pressure is shown as below:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<tr>\n",
    "<td> <img src=\"figures/property_IDT.png\" alt=\"Drawing\"/> </td>\n",
    "<td> <img src=\"figures/property_Tcurv_T.png\" alt=\"Drawing\"/> </td>\n",
    "</tr>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we need to get conditions set as below:\n",
    "``` python\n",
    "# in \"src/config.py:65-67\"\n",
    "phi_arr=[0.5, 1.0, 1.5]\n",
    "P_arr = [1.0, 10.0, 20.0]\n",
    "T_arr = [650., 700., 800., 1000., 1200.]\n",
    "```\n",
    "and by runing "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture\n",
    "%run src/config.py DME2000"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "we can get all conditions into file `mech/DME2000/conditions.txt`.\n",
    "\n",
    "Now we can use DRG method to generate skeletal mechanisms. At the first, we need the relationship between threshold and mechanism size (usually represented by the number of species). So, a series of DRG prune is performed under different threshold by running\n",
    "``` shell\n",
    "%run src/DRG.py mech=DME2000 test\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %run src/DRG.py mech=DME2000 test #this line is commented with intend"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For mechanism `DME2000.cti`, it often takes 1 CPU second to simulate one ignition. Since we have 54 conditions in each prune operation, it takes about 1 minute to run DRG once. In `src/DRG.py`, we first run threshold from 0.01 to 0.50 with interval as 0.01 in function `test()`. When mechanism is smaller, the simulation is faster with cubic relationship. And it takes about 26 minutes to finish this step.\n",
    "\n",
    "After that, we can run \n",
    "``` c++\n",
    "%run src/DRG.py DME2000 curv\n",
    "```\n",
    "to get the relationship plotted.\n",
    "\n",
    "<img width=\"60%\" src=\"figures/DRG_curv.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From this figure, we can choose threshold value to get skeletal mechanism with intended purpose. Here, threhold values 0.17, 0.31, 0.43 are chosen to get mechanisms with size 50, 43, 29.\n",
    "``` shell\n",
    "make DRG_reduce mech=DME2000 thresh=0.17\n",
    "make DRG_reduce mech=DME2000 thresh=0.31\n",
    "make DRG_reduce mech=DME2000 thresh=0.43\n",
    "```\n",
    "\n",
    "By running \n",
    "``` c++\n",
    "%run src/DRG.py DME2000 curv '[17,31,43]'\n",
    "```\n",
    "\n",
    "we can get the prune position:\n",
    "\n",
    "<img width=\"60%\" src=\"figures/DRG.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and the skeletal mechansims got are list as below:\n",
    "\n",
    "| Skeletal mechansim | Species No. | Reactions No. |\n",
    "|:------------------:|:-----------:|:-------------:|\n",
    "| DMEsk50            | 50          | 434           |\n",
    "| DMEsk43            | 43          | 362           |\n",
    "| DMEsk29            | 29          | 191           |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, we need to compare the skeletal mechanism and detailed mechanism by simulating the combustion properties (e.g. ignition delay time). By running `%run src/compare.py DME2000` we can get:\n",
    "\n",
    "IDT under different conditions as below:\n",
    "<img width=\"70%\" src=\"figures/compare_IDT.png\">\n",
    "\n",
    "Error under different conditions as below:\n",
    "<img width=\"70%\" src=\"figures/compare_err.png\">\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the main influence of mechanism reduction is truncation and coupling in reaction pathway. By running \n",
    "``` c++\n",
    "%run src/pathway.py DME2000\n",
    "```\n",
    "we can get "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<tr> <td>T=600K</td>  <td>T=1200K</td>  </tr>\n",
    "<tr>\n",
    "<td> <img src=\"figures/compare_Tcurv_mech_600.png\" alt=\"Drawing\"/></td>\n",
    "<td> <img src=\"figures/compare_Tcurv_mech_1200.png\" alt=\"Drawing\"/></td>\n",
    "</tr>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "which indicates that the influence between reduction is:\n",
    "\n",
    "| reduction          | high temperature pathway | low temperature pathway |\n",
    "| DME2000 -> DMEsk50 | "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Reference\n",
    "\n",
    "<span id=\"Lu2005\">Lu T, Law C K. A directed relation graph method for mechanism reduction[J]. Proceedings of the Combustion Institute, 2005, 30(1):1333-1341.</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
